package com.gedmanager.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CostsDataSource {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allColumns = { MySQLiteHelper.COST_ID,
			MySQLiteHelper.COST_NAME, MySQLiteHelper.COST_VALUE, MySQLiteHelper.COST_DATE, MySQLiteHelper.COST_COMMENT };

	public CostsDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Cost createCost(String name, String value, long date, String comment) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COST_NAME, name);
		values.put(MySQLiteHelper.COST_VALUE, value); 
		values.put(MySQLiteHelper.COST_DATE, date);
		values.put(MySQLiteHelper.COST_COMMENT, comment);
		long insertId = database.insert(MySQLiteHelper.TABLE_COST, null,
				values);
		Cursor cursor = database.query(MySQLiteHelper.TABLE_COST,
				allColumns, MySQLiteHelper.COST_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		Cost newCost = cursorToCost(cursor);
		cursor.close();
		return newCost;
	}

	public void deleteCost(long id) {    
		System.out.println("Cost deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_COST, MySQLiteHelper.COST_ID
				+ " = " + id, null);
	}

	public List<Cost> getAllCosts() {
		List<Cost> costs = new ArrayList<Cost>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_COST,
				allColumns, null, null, null, null, MySQLiteHelper.COST_DATE+" DESC");

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Cost cost = cursorToCost(cursor);
			costs.add(cost);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		return costs;
	}

	public List<Cost> getSubsetCosts(Date start, Date end) {
		List<Cost> costs = new ArrayList<Cost>();

		Log.d("aqui: ", "select ..");
		Cursor cursor = database.query(MySQLiteHelper.TABLE_COST,
				allColumns,  MySQLiteHelper.COST_DATE + "<?" + " AND " + MySQLiteHelper.COST_DATE + ">?", 
				new String[] {((Long)end.getTime()).toString(), ((Long)start.getTime()).toString()}, null, null, MySQLiteHelper.COST_DATE+" DESC");

		//Cursor cursor = database.rawQuery(sql, null);

		Log.d("aqui: ", "cursos .." + ((Long)end.getTime()).toString() + " " + ((Long)start.getTime()).toString() );
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Cost cost = cursorToCost(cursor);
			costs.add(cost);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		return costs;
	}

	private Cost cursorToCost(Cursor cursor) {
		Cost cost = new Cost();
		cost.setId(cursor.getLong(0));
		cost.setName(cursor.getString(1));
		cost.setValue(cursor.getString(2));
		cost.setDate(cursor.getLong(3));
		cost.setComment(cursor.getString(4));

		return cost;
	}

	public void updateCost(Cost cost) {

		ContentValues values = new ContentValues();
		//values.put(MySQLiteHelper.STORE_ID, store.getId());
		values.put(MySQLiteHelper.COST_NAME, cost.getName());
		values.put(MySQLiteHelper.COST_VALUE, cost.getValue());
		values.put(MySQLiteHelper.COST_DATE, cost.getDate());
		values.put(MySQLiteHelper.COST_COMMENT, cost.getComment());

		System.out.println("Cost updatd with id: " + cost.getId());

		Log.d("aqui update: ",
				((Long)cost.getId()).toString());
		database.update(MySQLiteHelper.TABLE_COST, values, MySQLiteHelper.COST_ID + "=" + ((Long)cost.getId()).toString(), null);


	}
	
	@SuppressWarnings("unused")
	public Cost getCost(long id) {
		try {
			Log.d("getcost", "inicio");
			Cursor cursor = database.query(MySQLiteHelper.TABLE_COST, allColumns,
					MySQLiteHelper.COST_ID + "=?", new String[] { ((Long) id).toString() }, null,
					null, null);
			Log.d("getcost dec", cursor.toString());
			if (cursor != null) {
				cursor.moveToFirst();
				Log.d("getcost dec", cursor.getString(1));
			}
			else { 
				return null;
			}

			Cost cost = cursorToCost(cursor);
			// make sure to close the cursor
			cursor.close();
			Log.d("getcost", "finish");
			return cost;
		}
		catch(Exception e) {
			e.printStackTrace();
		}

		return null;

	}
} 